package com.wuyan.locationdog.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.KeyEvent
import android.view.View
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.databinding.ActivityWebBinding
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient


class X5WebActivity : BaseBindingActivity<ActivityWebBinding>() {
    private lateinit var url: String

    /*** activity 跳转传值*/
    companion object {
        fun startAction(context: Context, url: String) {
            val intent = Intent(context, X5WebActivity::class.java)
            intent.putExtra("url", url)
            context.startActivity(intent)
        }
    }


    override fun initData() {
        setTitleText("")
        url = intent.getStringExtra("url") ?: ""

        Log.e("web", "url: $url")
        binding.apply {


            webView.settings.apply {
                setSupportZoom(true) //支持缩放，默认为true。是下面那个的前提。
                builtInZoomControls = true //设置内置的缩放控件。若为false，则该WebView不可缩放
//       displayZoomControls = true //隐藏原生的缩放控件
                blockNetworkImage = false //解决图片不显示
                loadsImagesAutomatically = true //支持自动加载图片
                defaultTextEncodingName = "utf-8" //设置编码格式

            }

            webView.loadUrl(url)
            //该界面打开更多链接
            webView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(webView: WebView, s: String?): Boolean {
                    webView.loadUrl(s)
                    return true
                }
            }

            //监听网页的加载进度
            webView.webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(webView: WebView, i: Int) {
                    progress.progress = i
                    if (i == 100) {
                        progress.visibility = View.GONE
                    }
                }

                override fun onReceivedTitle(p0: WebView?, title: String?) {
                    super.onReceivedTitle(p0, title)
                    if (title != null) {
                        setTitleText(title)
                    }
                }
            }
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    override fun onResume() {
        super.onResume();
        binding.apply {
            webView.onResume();
            webView.settings.javaScriptEnabled = true;
        }
    }

    override fun onPause() {
        super.onPause()
        binding.apply {
            webView.onPause()
            webView.settings.lightTouchEnabled = false
        }
    }

    //销毁 放置内存泄漏
    override fun onDestroy() {
        binding.apply {
            if (this.webView != null) {
                webView.destroy()
            }
        }
        super.onDestroy()
    }

    /**
     * 返回按鍵处理
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        binding.apply {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (webView != null && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                } else {
                    return super.onKeyDown(keyCode, event);
                }
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}